package cn.pli.bike.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.core.LatLonPoint;
import com.nostra13.universalimageloader.core.ImageLoader;

import cn.pli.bike.R;
import cn.pli.bike.bean.ExpensesRecordBean;
import cn.pli.bike.utils.AMapUtil;
import cn.pli.bike.utils.AppUtils;
import cn.pli.bike.utils.ScreenUtil;


/**
 * Created by Administrator on 2017/4/27.
 */

public class MyTripActivity extends Activity {
    private ExpensesRecordBean expensesRecordBean;
    MapView mMapView = null; //地图view
    ImageView image;
    private TextView tvTime, tvMoney;
    private ImageView ivBack;


    private AMap aMap;

    private BitmapDescriptor lineBitmap = null;
    private LatLonPoint mStartPoint = null;//起点，116.335891,39.942295
    private LatLonPoint mEndPoint = null;//终点，116.481288,39.995576

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_trip);
        mMapView = findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        aMap = mMapView.getMap();

        setTitle("我的行程");
        image = findViewById(R.id.profile_image);
        tvTime = findViewById(R.id.tv_ride_time);
        tvMoney = findViewById(R.id.tv_ride_money);
        ivBack = findViewById(R.id.iv_title_back);
        ivBack.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                finish();
                return false;
            }
        });


        expensesRecordBean = (ExpensesRecordBean) getIntent().getSerializableExtra("expensesRecordBean");

        mStartPoint = new LatLonPoint(expensesRecordBean.getBeginLatitude(), expensesRecordBean.getBeginLongitude());
        ;
        mEndPoint = new LatLonPoint(expensesRecordBean.getEndLatitude(), expensesRecordBean.getEndLongitude());
        setfromandtoMarker();
        addPolylinessoild();


    }

    //绘制一条实线
    private void addPolylinessoild() {
        LatLng A = new LatLng(expensesRecordBean.getBeginLatitude(), expensesRecordBean.getBeginLongitude());
      /*  LatLng B = new LatLng(Lat_B, Lon_B);
        LatLng C = new LatLng(Lat_C, Lon_C);*/
        LatLng D = new LatLng(expensesRecordBean.getEndLatitude(), expensesRecordBean.getEndLongitude());
        if (lineBitmap == null) {
            lineBitmap = BitmapDescriptorFactory
                    .fromResource(R.mipmap.ic_map_go);
        }
        aMap.addPolyline((new PolylineOptions())
                .add(A, /*B, C,*/ D)
                .width(18f)
                .setCustomTexture(lineBitmap));
        zoomToSpan();
    }

    private void setfromandtoMarker() {
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(mStartPoint))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.start)));
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(mEndPoint))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.end)));
    }

    public static void start(Context mContext) {
        Intent intent = new Intent(mContext, MyTripActivity.class);
        mContext.startActivity(intent);
    }

    /**
     * 移动镜头到当前的视角。
     *
     * @since V2.1.0
     */
    public void zoomToSpan() {
        try {
            LatLngBounds bounds = getLatLngBounds();
            aMap.animateCamera(CameraUpdateFactory
                    .newLatLngBounds(bounds, ScreenUtil.sysWidth(), ScreenUtil.sysHeight(), 50));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    protected LatLngBounds getLatLngBounds() {
        LatLngBounds.Builder b = LatLngBounds.builder();
        b.include(new LatLng(expensesRecordBean.getBeginLatitude(), expensesRecordBean.getBeginLongitude()));
        b.include(new LatLng(expensesRecordBean.getEndLatitude(), expensesRecordBean.getEndLongitude()));
        return b.build();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
        ImageLoader.getInstance().displayImage(AppUtils.getPhoto(), image);
        int time;
        long rideTime = expensesRecordBean.getEndTime() - expensesRecordBean.getBeginTime();
        if (rideTime < 0) {
            time = 1;
        } else {
            time = (int) (rideTime / (1000 * 60));
            time += 1;
        }

        tvTime.setText("骑行时间:" + time + "分钟   " + "骑行花费：" + expensesRecordBean.getExpensesAmount() + "元");
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

}
